package org.one.afternoon.utils;

import jakarta.websocket.Session;

import java.io.*;

// websocket 日志服务
public class TailLogUtil {
    //要查看的日志文件路径
    private static String logFilePath = System.getProperty("user.dir")+ File.separator+"log.txt";

    private static BufferedReader bufferedReader;

    public static void pushLog(Session session) {
        try {
            // 执行tail命令，获取输入流
            if (bufferedReader == null) {
                Process process = Runtime.getRuntime().exec("tail -f " + logFilePath);
                InputStream inputStream = process.getInputStream();
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            }
            // 通过WebSocket发送给实时日志给客户端
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                session.getBasicRemote().sendText(line + "<br />");
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}
